package com.example.parking.config;

import cn.hutool.json.JSONObject;
import com.example.parking.utils.Base64Util;
import com.example.parking.utils.FileUtil;
import com.example.parking.utils.HttpUtil;

import java.net.URLEncoder;

/**
 * 识别车牌图片——百度OCR
 */
public class LicensePlate {

    public static String licensePlate(String filePath) {
        // 请求url
        String url = "https://aip.baidubce.com/rest/2.0/ocr/v1/license_plate";
        try {
            byte[] imgData = FileUtil.readFileByBytes(filePath);
            String imgStr = Base64Util.encode(imgData);
            String imgParam = URLEncoder.encode(imgStr, "UTF-8");

            String param = "image=" + imgParam;

            //请求调用accessToken
            JSONObject jsonObject = Sample.getAccessToken();
            String accessToken = (String) jsonObject.get("access_token");
            String result = HttpUtil.post(url, accessToken, param);
//            System.out.println(result);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
